package org.murillo.chufa.entry;


import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;

public abstract class CacheEntry<K, V> extends SoftReference<V> {

//    private volatile K key;
    protected volatile V hardValue;
    protected long tickStamp;
    protected long creationCost;
    protected volatile int hits;
    protected float one_costxhits;

    protected CacheEntry(V referent, ReferenceQueue queue, long creationCost, long tickStamp) {
        super(referent, queue);
//        this.key = key;
        this.hits = 0;//new AtomicInteger(0);
        this.creationCost = creationCost;
        this.tickStamp = tickStamp;

        one_costxhits = 0.5F;
    }

//    protected CacheEntry(V referent, ReferenceQueue queue, long creationCost, SoftValueCacheEntry old) {
//        super(referent, queue);
////        this.key = key;
//        this.hits = old.hits;
//        this.creationCost = creationCost;
//        this.tickStamp = old.tickStamp;
//
//        one_costxhits = 0.5F;
//    }
    
    public abstract K getKey();
    
    public long getTickStamp() {
        return tickStamp;
    }

    public void setHard(V hard) {
        hardValue = hard;
    }

    public boolean isHard() {
        return hardValue != null;
    }

    public V getHard() {
        return hardValue;
    }

    public int getHits() {
        return hits;
    }

    public float getProfit(long tickStamp) {
        return (tickStamp - this.tickStamp) * one_costxhits;
    }

    public void hit() {
        int h = ++hits;//hits.incrementAndGet();
        one_costxhits = 1.0F / (creationCost * h + 1);
    }

    public abstract void dispose();
//        this.key = null;
//        this.hardValue = null;
   
}